package com.kedacom.demo.sigar;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;

import org.hyperic.sigar.Cpu;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetConnection;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.NetStat;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;
import org.hyperic.sigar.Swap;



public class SysInfo {  
  
    public static void main(String [] args) throws Exception{  
        SysInfo s =new SysInfo();  
       /* System.out.println("CPU个数："+s.getCpuCount());  
        System.out.println("--------getCpuTotal--------");
        s.getCpuTotal();  
        System.out.println("--------testCpuPerc--------");
        s.testCpuPerc();  
        System.out.println("--------testCpu--------");
        s.testCpu();
        System.out.println("--------getPhysicalMemory--------");
        s.getPhysicalMemory();  
        System.out.println("--------testWho--------");
        s.testWho();  
        System.out.println("--------testFileSystemInfo--------");
        s.testFileSystemInfo();  
        System.out.println("--------testGetOSInfo--------");
        s.testGetOSInfo();  
        System.out.println("--------getPlatformName--------"+s.getPlatformName());
        System.out.println("--------FQDN--------"+s.getFQDN());
        System.out.println("--------MAC--------"+s.getMAC());
        System.out.println("--------getDefaultIpAddress--------"+s.getDefaultIpAddress());
        s.getEthernetInfo();
        s.testNetIfList();*/
        s.getConnections();
        
        
    }  
    

    public void getConnections() throws SigarException {  
        Sigar sigar = new Sigar();  
        
        int flags = NetFlags.CONN_CLIENT |NetFlags.CONN_SERVER| NetFlags.CONN_PROTOCOLS;
        System.out.println("flags is "+flags);
        int proto_flags = 0;
        proto_flags |= NetFlags.CONN_TCP;
        proto_flags |= NetFlags.CONN_UDP;
        proto_flags |= NetFlags.CONN_RAW;
        proto_flags |= NetFlags.CONN_UNIX;

        if (proto_flags != 0) {
        flags &= ~NetFlags.CONN_PROTOCOLS;
        flags |= proto_flags;
        }
        System.out.println("flags is "+flags);
       

        
        NetConnection[] ncs = sigar.getNetConnectionList(flags);
        System.out.println("NetConnections:"+ncs.length);
        for(NetConnection nc:ncs){
        	System.out.println(nc);
        }
        NetStat ns = sigar.getNetStat();
        System.out.println("TcpInboundTotal:" + ns.getTcpInboundTotal());
        
        System.out.println("TcpOutboundTotal:" + ns.getTcpOutboundTotal());
        System.out.println("AllInboundTotal:" + ns.getAllInboundTotal());
        System.out.println("AllOutboundTotal:" + ns.getAllOutboundTotal());
        int [] stas = ns.getTcpStates();
        String s = "";
        for(int sta:stas){
        	s+=sta+",";
        }
        System.out.println("TcpStates:" + s);
        System.out.println("TcpEstablished:" + ns.getTcpEstablished());
        System.out.println("TcpSynSent:" + ns.getTcpSynSent());
        System.out.println("TcpSynRecv:" + ns.getTcpSynRecv());
        System.out.println("TcpFinWait1:" + ns.getTcpFinWait1());
        System.out.println("TcpFinWait2:" + ns.getTcpFinWait2());
        System.out.println("TcpTimeWait:" + ns.getTcpTimeWait());
        System.out.println("TcpClose:" + ns.getTcpClose());
        System.out.println("TcpCloseWait:" + ns.getTcpCloseWait());
        System.out.println("TcpLastAck:" + ns.getTcpLastAck());
        System.out.println("TcpListen:" + ns.getTcpListen());
        System.out.println("TcpClosing:" + ns.getTcpClosing());
        System.out.println("TcpIdle:" + ns.getTcpIdle());
        System.out.println("TcpBound:" + ns.getTcpBound());
        
        
       
    } 
    
    /** 
     * 1.CPU资源信息 
     */  
      
    // a)CPU数量（单位：个）  
    public int getCpuCount() throws SigarException {  
        Sigar sigar = new Sigar();  
        try {  
            return sigar.getCpuInfoList().length;  
        } finally {  
            sigar.close();  
        }  
    }  
  
    //获取系统总体CPU使用情况
    public void getCpuTotal(){
		Sigar sigar = new Sigar();
		Cpu cpu=null;
		try {
			cpu = sigar.getCpu();
		} catch (SigarException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("idle: " + cpu.getIdle());//获取整个系统cpu空闲时间
		System.out.println("irq: " + cpu.getIrq());
		System.out.println("nice: " + cpu.getNice());
		System.out.println("soft irq: " + cpu.getSoftIrq());
		System.out.println("stolen: " + cpu.getStolen());
		System.out.println("sys: " + cpu.getSys());
		System.out.println("total: " + cpu.getTotal());
		System.out.println("user: " + cpu.getUser());
		
		CpuPerc perc = null;
		try {
			perc = sigar.getCpuPerc();
		} catch (SigarException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("整体cpu的占用情况:");
		System.out.println("system idle: " + perc.getIdle());//获取当前cpu的空闲率
		System.out.println("conbined: "+ perc.getCombined());//获取当前cpu的占用率
    }
    
    // b)CPU的总量（单位：HZ）及CPU的相关信息  
    public void getCpuPerHz() {  
        Sigar sigar = new Sigar();  
        CpuInfo[] infos;  
        try {  
            infos = sigar.getCpuInfoList();  
            for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用  
                CpuInfo info = infos[i];  
                System.out.println("CPU的总量:" + info.getMhz());// CPU的总量MHz  
                System.out.println("获得CPU的卖主：" + info.getVendor());// 获得CPU的卖主，如：Intel  
                System.out.println("CPU的类别：" + info.getModel());// 获得CPU的类别，如：Celeron  
                System.out.println("缓冲存储器数量：" + info.getCacheSize());// 缓冲存储器数量  
                System.out.println(info);  
                System.out.println("**************");  
            }  
        } catch (SigarException e) {  
            e.printStackTrace();  
        }  
    }  
  
    // c)CPU的用户使用量、系统使用剩余量、总的剩余量、总的使用占用量等（单位：100%）  
    public void testCpuPerc() {  
        Sigar sigar = new Sigar();  
        // 方式一，主要是针对一块CPU的情况  
        CpuPerc cpu;  
        try {  
            cpu = sigar.getCpuPerc();  
            printCpuPerc(cpu);  
        } catch (SigarException e) {  
            e.printStackTrace();  
        }  
        // 方式二，不管是单块CPU还是多CPU都适用  
        CpuPerc cpuList[] = null;  
        try {  
            cpuList = sigar.getCpuPercList();  
        } catch (SigarException e) {  
            e.printStackTrace();  
            return;  
        }  
        for (int i = 0; i < cpuList.length; i++) {  
            printCpuPerc(cpuList[i]);  
        }  
    }  
    
    private void printCpuPerc(CpuPerc cpu) {  
        System.out.println("用户使用率:" + CpuPerc.format(cpu.getUser()));// 用户使用率  
        System.out.println("系统使用率:" + CpuPerc.format(cpu.getSys()));// 系统使用率  
        System.out.println("当前等待率:" + CpuPerc.format(cpu.getWait()));// 当前等待率  
        System.out.println("Nice :" + CpuPerc.format(cpu.getNice()));//  
        System.out.println("当前空闲率:" + CpuPerc.format(cpu.getIdle()));// 当前空闲率  
        System.out.println("总的使用率:" + CpuPerc.format(cpu.getCombined()));// 总的使用率  
        System.out.println("**************");  
    }  
    
    // c)CPU的用户使用量、系统使用剩余量、总的剩余量、总的使用占用量等（单位：long hz）
    public void testCpu() {  
        Sigar sigar = new Sigar();  
        Cpu[] cpus = null;
		try {
			cpus = sigar.getCpuList();
		} catch (SigarException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<cpus.length;i++){
			Cpu cpu = cpus[i];
			System.out.println("Cpu["+i+"]:"+cpu.toString());
		}
    }  
  
    /** 
     * 2.内存资源信息 
     * 
     */  
      
    public void getPhysicalMemory() {  
        // a)物理内存信息  
        DecimalFormat df = new DecimalFormat("#0.00");  
        Sigar sigar = new Sigar();  
        Mem mem;  
        try {  
            mem = sigar.getMem();  
            System.out.println("系统内存总量：" + df.format((float)mem.getTotal() / 1024/1024/1024) + "G");  
            System.out.println("物理内存总量：" + df.format((float)mem.getRam() / 1024) + "G"); 
            System.out.println("当前系统内存使用量：" + df.format((float)mem.getUsed() / 1024/1024/1024) + "G");  
            System.out.println("当前系统剩余内存量：" + df.format((float)mem.getFree() / 1024/1024/1024) + "G");
            System.out.println("实际占用物理内存量：" + df.format((float)mem.getActualUsed() / 1024/1024/1024) + "G");  
            System.out.println("实际空闲物理内存量：" + df.format((float)mem.getActualFree() / 1024/1024/1024) + "G");  
            System.out.println("系统内存使用率：" + df.format((float)mem.getUsedPercent()*100D)+ "%");  
            System.out.println("系统内存空闲率：" + df.format((float)mem.getFreePercent()*100D) + "%");  
            // b)系统页面文件交换区信息  
            Swap swap = sigar.getSwap();  
            // 交换区总量  
            System.out.println("交换区总量：" + df.format((float)swap.getTotal() / 1024/1024/1024) + "G");  
            // 当前交换区使用量  
            System.out.println("交换区使用量：" + df.format((float)swap.getUsed() / 1024/1024/1024) + "G");  
            // 当前交换区剩余量  
            System.out.println("交换区空闲量：" + df.format((float)swap.getFree() / 1024/1024/1024) + "G");  
            System.out.println("交换区页面置入：" + swap.getPageIn()); 
            System.out.println("交换区页面置出：" + swap.getPageOut()); 
        } catch (SigarException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * 3.操作系统信息 
     *  
     */  
      
    // a)取到当前操作系统的名称：  
    public String getPlatformName() {  
        String hostname = "";  
        try {  
            hostname = InetAddress.getLocalHost().getHostName();  
        } catch (Exception exc) {  
            Sigar sigar = new Sigar();  
            try {  
                hostname = sigar.getNetInfo().getHostName();  
            } catch (SigarException e) {  
                hostname = "localhost.unknown";  
            } finally {  
                sigar.close();  
            }  
        }  
        return hostname;  
    }  
  
    // b)取当前操作系统的信息  
    public void testGetOSInfo() {  
        OperatingSystem OS = OperatingSystem.getInstance();  
        // 操作系统内核类型如： 386、486、586等x86  
        System.out.println("OS.getArch() = " + OS.getArch());  
        System.out.println("OS.getCpuEndian() = " + OS.getCpuEndian());//  
        System.out.println("OS.getDataModel() = " + OS.getDataModel());//  
        // 系统描述  
        System.out.println("OS.getDescription() = " + OS.getDescription());  
        System.out.println("OS.getMachine() = " + OS.getMachine());//  
        // 操作系统类型  
        System.out.println("OS.getName() = " + OS.getName());  
        System.out.println("OS.getPatchLevel() = " + OS.getPatchLevel());//  
        // 操作系统的卖主  
        System.out.println("OS.getVendor() = " + OS.getVendor());  
        // 卖主名称  
        System.out  
                .println("OS.getVendorCodeName() = " + OS.getVendorCodeName());  
        // 操作系统名称  
        System.out.println("OS.getVendorName() = " + OS.getVendorName());  
        // 操作系统卖主类型  
        System.out.println("OS.getVendorVersion() = " + OS.getVendorVersion());  
        // 操作系统的版本号  
        System.out.println("OS.getVersion() = " + OS.getVersion());  
    }  
  
    // c)取当前系统进程表中的用户信息  
    public void testWho() {  
        try {  
            Sigar sigar = new Sigar();  
            org.hyperic.sigar.Who[] who = sigar.getWhoList();  
            if (who != null && who.length > 0) {  
                for (int i = 0; i < who.length; i++) {  
                    System.out.println("\n~~~~~~~~~" + String.valueOf(i)  
                            + "~~~~~~~~~");  
                    org.hyperic.sigar.Who _who = who[i];  
                    System.out.println("获取设备getDevice() = " + _who.getDevice());  
                    System.out.println("获得主机getHost() = " + _who.getHost());  
                    System.out.println("获取的时间getTime() = " + _who.getTime());  
                    // 当前系统进程表中的用户名  
                    System.out.println("获取用户getUser() = " + _who.getUser());  
                }  
            }  
        } catch (SigarException e) {  
            e.printStackTrace();  
        }  
    }  
  
    // 4.资源信息（主要是硬盘）  
    // a)取硬盘已有的分区及其详细信息（通过sigar.getFileSystemList()来获得FileSystem列表对象，然后对其进行编历）：  
    public void testFileSystemInfo() throws Exception {  
        Sigar sigar = new Sigar();  
        FileSystem fslist[] = sigar.getFileSystemList();  
        DecimalFormat df = new DecimalFormat("#0.00");  
        // String dir = System.getProperty("user.home");// 当前用户文件夹路径  
        for (int i = 0; i < fslist.length; i++) {  
            System.out.println("\n~~~~~~~~~~" + i + "~~~~~~~~~~");  
            FileSystem fs = fslist[i];  
            // 分区的盘符名称  
            System.out.println("fs.getDevName() = " + fs.getDevName());  
            // 分区的盘符名称  
            System.out.println("fs.getDirName() = " + fs.getDirName());  
            System.out.println("fs.getFlags() = " + fs.getFlags());//  
            // 文件系统类型，比如 FAT32、NTFS  
            System.out.println("fs.getSysTypeName() = " + fs.getSysTypeName());  
            // 文件系统类型名，比如本地硬盘、光驱、网络文件系统等  
            System.out.println("fs.getTypeName() = " + fs.getTypeName());  
            // 文件系统类型  
            System.out.println("fs.getType() = " + fs.getType());  
            System.out.println("fs.getOptions() = " + fs.getOptions());  
            
            
            
            FileSystemUsage usage = null;  
            try {  
                usage = sigar.getFileSystemUsage(fs.getDirName());  
            } catch (SigarException e) {  
                if (fs.getType() == 2)  
                    throw e;  
                continue;  
            }  
            switch (fs.getType()) {  
            case 0: // TYPE_UNKNOWN ：未知  
                break;  
            case 1: // TYPE_NONE  
                break;  
            case 2: // TYPE_LOCAL_DISK : 本地硬盘  
                // 文件系统总大小  
                System.out.println(" Total = " + df.format((float)usage.getTotal()/1024/1024) + "G");  
                // 文件系统剩余大小  
                System.out.println(" Free = " + df.format((float)usage.getFree()/1024/1024) + "G");  
                // 文件系统可用大小  
                System.out.println(" Avail = " + df.format((float)usage.getAvail()/1024/1024) + "G");  
                // 文件系统已经使用量  
                System.out.println(" Used = " + df.format((float)usage.getUsed()/1024/1024) + "G");                  
                double usePercent = usage.getUsePercent() * 100D;  
                // 文件系统资源的利用率  
                System.out.println(" Usage = " + df.format(usePercent) + "%");  
                
                System.out.println(" Files = " + usage.getFiles()); 
                System.out.println(" FreeFiles = " + usage.getFreeFiles()); 

                
                
                break;  
            case 3:// TYPE_NETWORK ：网络  
                break;  
            case 4:// TYPE_RAM_DISK ：闪存  
                break;  
            case 5:// TYPE_CDROM ：光驱  
                break;  
            case 6:// TYPE_SWAP ：页面交换  
                break;  
            }  
            System.out.println(" DiskReads = " + usage.getDiskReads());  
            System.out.println(" DiskWrites = " + usage.getDiskWrites()); 
            System.out.println(" DiskReadBytes = " + usage.getDiskReadBytes()/1024 + "KB"); 
            System.out.println(" DiskWriteBytes = " + usage.getDiskWriteBytes()/1024 + "KB"); 
            System.out.println(" DiskQueue = " + usage.getDiskQueue()); 
            System.out.println(" DiskServiceTime = " + usage.getDiskServiceTime()); 
        }  
        return;  
    }  
  
    // 5.网络信息  
    // a)当前机器的正式域名  
    public String getFQDN() {  
        Sigar sigar = null;  
        try {  
            return InetAddress.getLocalHost().getCanonicalHostName();  
        } catch (UnknownHostException e) {  
            try {  
                sigar = new Sigar();  
                return sigar.getFQDN();  
            } catch (SigarException ex) {  
                return null;  
            } finally {  
                sigar.close();  
            }  
        }  
    }  
  
    // b)取到当前机器的IP地址  
    public String getDefaultIpAddress() {  
        String address = null;  
        try {  
            address = InetAddress.getLocalHost().getHostAddress();  
            // 没有出现异常而正常当取到的IP时，如果取到的不是网卡循回地址时就返回  
            // 否则再通过Sigar工具包中的方法来获取  
            if (!NetFlags.LOOPBACK_ADDRESS.equals(address)) {  
                return address;  
            }  
        } catch (UnknownHostException e) {  
            // hostname not in DNS or /etc/hosts  
        }  
        Sigar sigar = new Sigar();  
        try {  
            address = sigar.getNetInterfaceConfig().getAddress();  
        } catch (SigarException e) {  
            address = NetFlags.LOOPBACK_ADDRESS;  
        } finally {  
            sigar.close();  
        }  
        return address;  
    }  
  
    // c)取到当前机器的MAC地址  
    public String getMAC() {  
        Sigar sigar = null;  
        try {  
            sigar = new Sigar();  
            String[] ifaces = sigar.getNetInterfaceList();  
            print("ifaces = " + ifaces[0]);
            String hwaddr = null;  
            for (int i = 0; i < ifaces.length; i++) {  
                NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);  
                if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())  
                        || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0  
                        || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {  
                    continue;  
                }  
                /* 
                 * 如果存在多张网卡包括虚拟机的网卡，默认只取第一张网卡的MAC地址，如果要返回所有的网卡（包括物理的和虚拟的）则可以修改方法的返回类型为数组或Collection 
                 * ，通过在for循环里取到的多个MAC地址。 
                 */  
                hwaddr = cfg.getHwaddr();  
                break;  
            }  
            return hwaddr != null ? hwaddr : null;  
        } catch (Exception e) {  
            return null;  
        } finally {  
            if (sigar != null)  
                sigar.close();  
        }  
    }  
  
    // d)获取网络流量等信息  
    public void testNetIfList() throws Exception {  
        Sigar sigar = new Sigar();  
        String ifNames[] = sigar.getNetInterfaceList();  
        for (int i = 0; i < ifNames.length; i++) {  
            String name = ifNames[i];  
            NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);  
            print("\nname = " + name);// 网络设备名  
            print("Address = " + ifconfig.getAddress());// IP地址  
            print("Netmask = " + ifconfig.getNetmask());// 子网掩码  
            if ((ifconfig.getFlags() & 1L) <= 0L) {  
                print("!IFF_UP...skipping getNetInterfaceStat");  
                continue;  
            }  
            try {  
                NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);  
                print("RxPackets = " + ifstat.getRxPackets());// 接收的总包裹数  
                print("RxBytes = " + ifstat.getRxBytes());// 接收到的总字节数  
                print("RxErrors = " + ifstat.getRxErrors());// 接收到的错误包数  
                print("RxDropped = " + ifstat.getRxDropped());// 接收时丢弃的包数 
                print("RxOverruns = " + ifstat.getRxOverruns());
                print("RxRxFrame = " + ifstat.getRxFrame()); 
                
                
                print("TxPackets = " + ifstat.getTxPackets());// 发送的总包裹数  
                print("TxBytes = " + ifstat.getTxBytes());// 发送的总字节数  
                print("TxErrors = " + ifstat.getTxErrors());// 发送数据包时的错误数  
                print("TxDropped = " + ifstat.getTxDropped());// 发送时丢弃的包数
                print("TxOverruns = " + ifstat.getTxOverruns());
                print("TxCollisions = " + ifstat.getTxCollisions()); 
                print("TxCarrier = " + ifstat.getTxCarrier()); 
                
                print("Speed = " + ifstat.getSpeed()); 
                
            } catch (SigarNotImplementedException e) {  
            } catch (SigarException e) {  
                print(e.getMessage());  
            }  
        }  
    }  
  
    void print(String msg) {  
        System.out.println(msg);  
    }  
  
    // e)一些其他的信息  
    public void getEthernetInfo() {  
        Sigar sigar = null;  
        try {  
            sigar = new Sigar();  
            String[] ifaces = sigar.getNetInterfaceList();  
            for (int i = 0; i < ifaces.length; i++) {  
                NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);  
                if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress())  
                        || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0  
                        || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {  
                    continue;  
                }  
                System.out.println("cfg.getAddress() = " + cfg.getAddress());// IP地址  
                System.out  
                        .println("cfg.getBroadcast() = " + cfg.getBroadcast());// 网关广播地址  
                System.out.println("cfg.getHwaddr() = " + cfg.getHwaddr());// 网卡MAC地址  
                System.out.println("cfg.getNetmask() = " + cfg.getNetmask());// 子网掩码  
                System.out.println("cfg.getDescription() = "  
                        + cfg.getDescription());// 网卡描述信息  
                System.out.println("cfg.getType() = " + cfg.getType());//  
                System.out.println("cfg.getDestination() = "  
                        + cfg.getDestination());  
                System.out.println("cfg.getFlags() = " + cfg.getFlags());//  
                System.out.println("cfg.getMetric() = " + cfg.getMetric());  
                System.out.println("cfg.getMtu() = " + cfg.getMtu());  
                System.out.println("cfg.getName() = " + cfg.getName());  
                System.out.println();  
            }  
        } catch (Exception e) {  
            System.out.println("Error while creating GUID" + e);  
        } finally {  
            if (sigar != null)  
                sigar.close();  
        }  
    }  
  
}  